package com.fms.model.domain.messaging;

import java.util.ArrayList;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;

import com.fms.model.domain.FlowSheet;
import com.fms.model.messaging.IMessager;
import com.fms.model.messaging.MessageDispatcher;
import com.fms.model.messaging.domain.DraftMessage;
import com.fms.model.messaging.domain.IEmailData;
import com.fms.model.messaging.repo.IEmailDataRepository;

public class MessageDispatcherTest extends TestCase
{
	private Mockery mock;
	
	public void setUp()
	{
		mock = new Mockery();
	}
	
	
	public void tearDown()
	{
		mock.assertIsSatisfied();
	}
	
	public void testMessageFilledAndSent()
	{
		final IMessager messager = mock.mock(IMessager.class);
		final IEmailData data = mock.mock(IEmailData.class);
		final IEmailDataRepository repo = mock.mock(IEmailDataRepository.class);
		
		final DraftMessage message = new DraftMessage();
		message.setFrom("courtney.whitebread");
		message.addTo("bryan.johnson@hotmail.com");
		message.addTo("jpl@hotmail.com");
		message.setMessage("A Message here!");
		message.setSubject("A subject here!");
		message.setSheet(new FlowSheet("FileName"));
		
		final ArrayList<String> toData = new ArrayList<String>();
		toData.add("bryan.johnson@hotmail.com");
		toData.add("jpl@hotmail.com");
		
		
		try
		{
			mock.checking(new Expectations()
			{{
				oneOf(repo).load(); will(returnValue(data));
				oneOf(messager).send(with(new DraftMatch(message)));
				oneOf(data).getDestinations(); will(returnValue(toData));
				oneOf(data).getMessage(); will(returnValue("A Message here!"));
				oneOf(data).getSubject(); will(returnValue("A subject here!"));
				oneOf(data).getFlowSheet(); will(returnValue(new FlowSheet("FileName")));
				oneOf(repo).save(with(data));
				
			}});
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		MessageDispatcher d = new MessageDispatcher(messager, repo, null);
		d.sendMail();
		
		
	}
	
	public void testMessageFilledAndSentNoChanges()
	{
		final IMessager messager = mock.mock(IMessager.class);
		final IEmailData data = mock.mock(IEmailData.class);
		final IEmailDataRepository repo = mock.mock(IEmailDataRepository.class);
		
		final DraftMessage message = new DraftMessage();
		message.addTo("bryan.johnson@hotmail.com");
		message.addTo("jpl@hotmail.com");
		message.setMessage("A Message here!");
		message.setSubject("A subject here!");
		message.setSheet(new FlowSheet("FileName"));
		
		final ArrayList<String> toData = new ArrayList<String>();
		toData.add("bryan.johnson@hotmail.com");
		toData.add("jpl@hotmail.com");
		
		
		try
		{
			mock.checking(new Expectations()
			{{
				
				oneOf(repo).load(); will(returnValue(data));
				oneOf(messager).send(with(new DraftMatch(message)));
				oneOf(data).getDestinations(); will(returnValue(toData));
				oneOf(data).getMessage(); will(returnValue("A Message here!"));
				oneOf(data).getSubject(); will(returnValue("A subject here!"));
				oneOf(data).getFlowSheet(); will(returnValue(new FlowSheet("FileName")));
				oneOf(repo).save(with(data));
				
			}});
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		MessageDispatcher d = new MessageDispatcher(messager, repo, null);
		
		d.sendMail();
		
		
		
		
	}
}
